﻿using System;
using System.Collections.Generic;
using System.Web.UI;
using System.Web.UI.HtmlControls;

namespace OpenWaves.EPiServer.Comments.Content.ContentWrappers
{
    public class DefaultContentWrapper : IContentWrapper
    {
        public virtual Control WrapContent(Control contentToBeWrapped)
        {
            var contentTemplateWrapper =
                CreateDivControlWithDataBindAndClassAttributes(
                    dataBindAttribute: "visible: LoadingInProgress() == false",
                    classAttribute: "ow-epi-comments-container");

            return WrapContent(contentTemplateWrapper, contentToBeWrapped);
        }


        public virtual Control WrapLoadingInProgressContent(Control contentToBeWrapped)
        {
            var loadingInProgressTemplateWrapper =
                CreateDivControlWithDataBindAndClassAttributes(
                    dataBindAttribute: "visible: LoadingInProgress",
                    classAttribute: "ow-epi-loading-in-progress-container");

            return WrapContent(loadingInProgressTemplateWrapper, contentToBeWrapped);
        }



        protected virtual Control CreateDivControlWithDataBindAndClassAttributes(string dataBindAttribute,
                                                                                 string classAttribute)
        {
            return CreateGenericControlWithAttributes(
                "div",
                new KeyValuePair<string, string>("data-bind", dataBindAttribute),
                new KeyValuePair<string, string>("class", classAttribute));
        }


        protected virtual Control CreateGenericControlWithAttributes(
            string tag, 
            params KeyValuePair<string, string>[] attributes)
        {
            var control = new HtmlGenericControl(tag);

            foreach(var attribute in attributes)
                control.Attributes.Add(attribute.Key, attribute.Value);

            return control;
        }


        protected virtual Control WrapContent(Control wrapper, Control contentToBeWrapped)
        {
            if (wrapper == null || contentToBeWrapped == null)
                return wrapper;

            wrapper.Controls.Add(contentToBeWrapped);

            return wrapper;
        }
    }
}
